<?php

namespace App\Service\Model;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;



/**
 * @property \Illuminate\Database\Eloquent\Collection $details
 */
class Orders extends \Eloquent
{
    const status_init    = 0;//待确认
    const status_default = 1;//执行中
    const status_success = 2;//执行结束
    const status_failed  = 3;//执行失败
    
    
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The database used by the model.
     *
     * @var string
     */
    protected $table = 'orders';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [''];
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [''];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    public function __construct(array $attributes = [])
    {
        loadLibrary('common');
        parent::__construct($attributes);
    }


    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * 获取订单详情
    */
    public function details(){
        return $this->hasMany(OrderDetails::class,'order_id');
    }
    /**
     * 创建一个订单
     *
     * @param [type] $orderId
     * @param [type] $amount
     * @param [type] $type
     * @return void
     */
    public static function createOrder($cardno , $orderId , $amount, $type , $acctname)
    {
        self::create([
            'acctname' => $acctname,
            'cardno'   => $cardno,
            'amount'   => $amount,
            'type'     => $type,
            'uid'      => user()->getKey(),
            'order_id' => $orderId,
            'status'   => self::status_default,
        ]);
    }

    /**
     * 获取已到期 未执行的订单
     *
     * @return void
     */
    public static function getOrdersExecute(){
        //return self::where('status',self::status_default)->where('execute_time','<=',Carbon::now())->get();
        $orderIds = self::where('status',self::status_default)->get('id')->toArray();
        $orderDetails = OrderDetails::whereIn('order_id',array_column($orderIds,'id'))->where('status',[self::status_default])
            ->where('execute_time','<=',Carbon::now())->get();
        return $orderDetails;
    }
    /**
     * 查询订单
     *
     * @param [type] $orderId
     * @return void
     */
    public static function getOrderByNo($orderId)
    {
        return self::where('order_id', $orderId)->first();
    }
    /**
     * 订单交易成功
     *
     * @return void
     */
    public function notifySuccess($ret)
    {
        $this->status      = self::status_success;
        $this->notify      = objectToString($ret);
        $this->notify_time = Carbon::now();
        $this->saveOrFail();
    }

    

    /**
     * 获取未执行 或者 执行失败 的任务
     *
     * @param [type] $cardno
     * @return void
     */
    public static function getUndoneOrders($cardno){
        return self::where('cardno',$cardno)->whereIn('status',[self::status_default,self::status_failed])->get();
    }

    
    public function getCardnoAttribute($value)
    {
        return hide_middle($value);
    }

    public function getPlanFinishDateAttribute($value){
        return substr($value,0,16);
    }
}
